<?php

namespace App\Http\Controllers\F;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\FinanceAccount;
use App\Models\FinanceRecord;
use App\Contant\FinanceRecord as FinanceRecordType;
use App\User;
use Excel;

class FinanceController extends Controller
{
    public function __construct()
    {

    }

    public function index(Request $request){
        $financeRecord = new FinanceRecord();
        $userName = $request->input('name','');
        $author = $request->input('author','');
        $type = $request->input('type',null);
        $start = $request->input('start');
        $end = $request->input('end');
        if($userName){

            $users = User::where('name','like','%'.$userName.'%')->where('is_admin',0)->select('id')->get()->toArray();
            $userArr = [];
            foreach($users as $user){
                $userArr[] = $user['id'];
            }
            $financeRecord = $financeRecord->whereIn('user_id',$userArr);
        }
        if($author){
            $authors = User::where('name','like','%'.$author.'%')->where('is_admin',1)->select('id')->get();
            $courseArr = [];
            foreach($authors as $au){
                $courseClass = $au->courseClass;
                foreach($courseClass as $course){
                    $courseArr[] = $course->id;
                }
            }
            if($courseArr){
                $financeRecord = $financeRecord->whereIn('course_class_id',$courseArr);
            }
        }
        if($start){
            $financeRecord = $financeRecord->where('created_at','>=',$start);
        }
        if($end){
            $financeRecord = $financeRecord->where('created_at','<=',$end);
        }
        if($type != null && $type == 0){
            $financeRecord = $financeRecord->where('payfrom','BALANCE');
        }elseif($type == 1){
            $financeRecord = $financeRecord->whereIn('payfrom',['WXPAY','WXPAYH']);
        }
        $financeRecord = $financeRecord->where('type','BUY');
        $financeRecords = $financeRecord->orderBy('id','desc')->paginate(10);
        return view('finance.index',['financeRecords'=>$financeRecords,'type'=>$type,'userName'=>$userName,'author'=>$author,'start'=>$start,'end'=>$end]);
    }

    public function flowExport(Request $request){
//        dd(3);
        $financeRecord = new FinanceRecord();
        $userName = $request->input('name','');
        $author = $request->input('author','');
        $type = $request->input('type',null);
        $start = $request->input('start');
        $end = $request->input('end');
        if($userName){

            $users = User::where('name','like','%'.$userName.'%')->where('is_admin',0)->select('id')->get()->toArray();
            $userArr = [];
            foreach($users as $user){
                $userArr[] = $user['id'];
            }
            $financeRecord = $financeRecord->whereIn('user_id',$userArr);
        }
        if($author){
            $authors = User::where('name','like','%'.$author.'%')->where('is_admin',1)->select('id')->get();
            $courseArr = [];
            foreach($authors as $au){
                $courseClass = $au->courseClass;
                foreach($courseClass as $course){
                    $courseArr[] = $course->id;
                }
            }
            if($courseArr){
                $financeRecord = $financeRecord->whereIn('course_class_id',$courseArr);
            }
        }
        if($start){
            $financeRecord = $financeRecord->where('created_at','>=',$start);
        }
        if($end){
            $financeRecord = $financeRecord->where('created_at','<=',$end);
        }
        if($type != null && $type == 0){
            $financeRecord = $financeRecord->where('payfrom','BALANCE');
        }elseif($type == 1){
            $financeRecord = $financeRecord->whereIn('payfrom',['WXPAY','WXPAYH']);
        }
        $financeRecord = $financeRecord->where('type','BUY');
        $financeRecords = $financeRecord->orderBy('id','desc')->get();
        $financeData = ['付费者','导师','手机号','支付方式','付款时间','金额','项目'];
        $total = 0;
        foreach($financeRecords as $k=>$record){
            if($record->user){
                $info[$k][] = $record->user->name;
            }else{
                $info[$k][] = '';
            }
            if($record->courseClass && $record->courseClass->author){
                $info[$k][] = $record->courseClass->author->name;
            }else{
                $info[$k][] = '';
            }
            if($record->user){
                $info[$k][] = "".$record->user->mobile."";
            }else{
                $info[$k][] = '';
            }
            $info[$k][] = FinanceRecordType::trans($record->payfrom);
            $info[$k][] = date('Y-m-d H:i:s',strtotime($record->created_at));
            $info[$k][] = $record->money;
            if($record->courseClass){
                $info[$k][] = $record->courseClass->title;
            }else{
                $info[$k][] = '';
            }
            $total += $record->money;
        }
        array_unshift($info,$financeData);
//        dd($info);

        $data['list'] = $info;
        $data['total'] = $total;
//        dd($data);
        Excel::create('流水报表', function($excel) use($data) {
            $data = $data;
            $excel->sheet('流水', function($sheet) use($data) {
                $infos = $data['list'];
                $length = count($infos);
                $sheet->setWidth(array(
                    'A' =>  20,
                    'B' =>  20,
                    'c' => 20,
                    'D' => 20,
                    'E' => 20,
                    'F' => 20,
                    'G'=>70
                ));


//                dd($data['total']);
                for($i=0;$i<$length;$i++){
                    $sheet->row($i+1,$infos[$i]);
                }
                $sheet->mergeCells('A'.($length+1).':F'.($length+1));
                $sheet->row($length+1, ['总计：']);
                $total = $data['total'];
                $sheet->cell('G'.($length+1),function($cell) use($total){
                    $cell->setValue($total);
                });
//                $sheet->row($length,['总计',$data['total']]);
//                dd(['总计',$data['total']]);

            });

        })->export('xls');
    }

}
